/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.servlets;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Vocabulary;

import org.myontology.users.Login;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;

import controller.sesameDataConnector.AbstractDataConnector;

/**
 * Servlet implementation class for Servlet: ShowFreezePoint
 *
 */
 public class ShowFreezePoint extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ShowFreezePoint() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		
		
		if ( !Login.LOGGED_IN.getValue(request) ) {			
			response.sendRedirect( request.getContextPath() + "/login");
			return;
		}
		
		String filepath = getFilePath( request.getParameter("id"), request.getParameter("freezepointid") );        

		if( filepath!=null ) {
			
			File myfile = new File( filepath );
			
			if( myfile.canRead() ) {
		
				BufferedReader br = new BufferedReader(
						  new InputStreamReader(
						    new FileInputStream( myfile ) ) );
		
			    PrintWriter out = response.getWriter();
				
				String row = br.readLine(); 
				while (row != null) { 
					out.println( row );
					row = br.readLine(); 
				} 
			}
		}
		
		return;
	}
	
	private String getFilePath( String ontologyid, String freezepointid ) {
		
		/*
		 Get to know the local Filename:
			1.	request filename:
			a.	pseudo SeQRL: SELECT f FROM <generated ontology URI> hasFreezePoint <generated  sequenceURI> filename f
			2.	/usr/local/myontologyfreezepoints/<FILENAME> (there the files are stored on the server; this path shoud be determined via web.xml of tomcat server)
		 */
		
		String filepath = null; // should be generated out of SesameDatadirectory...
		try {
			
			URI ontologyUri = AbstractDataConnector.getURI( ontologyid );
			URI freezepointUri = new URIImpl( ontologyUri.stringValue() + "/Frozen/" + freezepointid );
			
			String filename = AbstractDataConnector.selectLiteralObject( freezepointUri, Vocabulary.FILENAME, null );
			
			if( filename!=null ) {
				
				filepath = this.getServletContext().getInitParameter( "FreezePointsDataDirectory" ) + File.separator + filename;
			}
		
		}
		catch( Exception e ) {
			
			e.printStackTrace();
		}
		
    
		return filepath;
	}
	  	    
}
